<template>
  <div class="app-container">
    <!-- 搜索区域设置 -->
    <el-row v-if="searchToggle" :gutter="20">
      <el-col>
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入要查询权限名称/系统页面"
              clearable
              prefix-icon="el-icon-search"
              @keyup.enter.native="handleQuery"
              @clear="handleQuery"
            />
          </el-form-item>
          <el-form-item prop="menuId">
            <!-- <el-input
              v-model="queryParams.menuId"
              placeholder="请输入要查询权限名称/系统页面"
              clearable
              prefix-icon="el-icon-search"
              @keyup.enter.native="handleQuery"
              @clear="handleQuery"
            /> -->
            <el-select
              v-model="queryParams.menuId"
              placeholder="请选择系统菜单"
              clearable
              filterable
              default-first-option
              @clear="queryParams.menuId = undefined"
            >
              <el-option
                v-for="item in menuOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <!-- icon="el-icon-search" -->
            <el-button type="primary" @click="handleQuery" icon="el-icon-search"
              >搜索</el-button
            >
            <!-- icon="el-icon-refresh" -->
            <el-button @click="handleReset">重置 </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <!-- 相关操作按钮 -->

    <el-row type="flex" class="mb8">
      <!-- icon="el-icon-plus" -->
      <el-button
        v-permission="['PRIV_POWERS_CREATE']"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate()"
        >新增</el-button
      >
      <el-button
        type="primary"
        icon="el-icon-folder-opened"
        @click="toggleRowExpansion(true)"
        >全部展开</el-button
      >
      <el-button
        type="primary"
        icon="el-icon-folder"
        @click="toggleRowExpansion(false)"
        >全部折叠</el-button
      >
      <el-button-group class="ml">
        <el-button
          size="mini"
          plain
          type="info"
          icon="el-icon-search"
          @click="handleSearch"
        />
        <el-button size="mini" icon="el-icon-refresh" @click="handleQuery" />
      </el-button-group>
    </el-row>

    <!-- 表格内容设置 -->

    <el-row>
      <!--:default-sort="{ prop: queryParams.orderby, order: queryParams.sort }"  -->
      <el-table
        v-loading="loading"
        :height="tableHeight"
        :data="dataResult"
        row-key="id"
        stripe
        border
        ref="dataTreeList"
        default-expand-all
        @sort-change="handleSortable"
        :tree-props="{ hasChildren: 'hasChildren', children: 'powers' }"
      >
        <el-table-column
          type="index"
          :index="handleIndexCalc"
          label="#"
          align="center"
        />
        <el-table-column
          prop="name"
          label="权限标识"
          sortable
          :show-overflow-tooltip="true"
        />
        <!-- <el-table-column prop="page" label="系统页面" sortable align="center" /> -->
        <el-table-column prop="description" label="权限描述" align="center" />
        <el-table-column
          prop="remark"
          :show-overflow-tooltip="true"
          label="备注"
          align="center"
        />
        <el-table-column
          prop="updateTime"
          label="创建时间"
          sortable
          align="center"
        />
        <el-table-column
          label="操作"
          align="center"
          width="200"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <!--  -->
            <div v-if="!scope.row.powers">
              <el-button-group>
                <!-- icon="el-icon-edit" -->
                <el-button
                  class="edit_list_btn list_btn"
                  v-permission="['PRIV_POWERS_UPDATE']"
                  type="info"
                  size="mini"
                  @click="handleUpdate(scope.row, scope)"
                  icon="el-icon-edit-outline"
                  >编辑</el-button
                >
                <!--  icon="el-icon-delete" -->
                <el-button
                  class="del_list_btn list_btn"
                  v-permission="['PRIV_POWERS_DELETE']"
                  type="danger"
                  size="mini"
                  @click="handleDelete(scope.row)"
                  icon="el-icon-delete"
                  >删除</el-button
                >
              </el-button-group>
            </div>
            <div v-else>---</div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageIndex"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>

    <!-- 添加或修改权限对话框 -->

    <m-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="系统页面" prop="menuId">
              <el-select
                v-model="form.menuId"
                placeholder="请选择权限描述"
                clearable
                filterable
              >
                <el-option
                  v-for="item in menuOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
              <!-- <treeselect
                v-model="form.page"
                value="name"
                :load-options="loadOptions"
                :options="menuOptions"
                :normalizer="normalizer"
                :show-count="true"
                no-results-text="没有查询到菜单"
                placeholder="请输入权限所属的系统页面"
              /> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="权限名称" maxlength="50" prop="name">
              <el-input
                v-model.trim="form.name"
                placeholder="请输入权限名,称命令规范（PRIV_[页面]_[动作]）"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="权限描述" prop="description">
              <!-- <el-select
                v-model="form.description"
                placeholder="请选择权限描述"
                clearable
                filterable
                allow-create
                default-first-option
              >
                <el-option
                  v-for="item in descriptionOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select> -->
              <el-input
                v-model.trim="form.description"
                placeholder="请选择权限描述"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="权限备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                :autosize="{ minRows: 2 }"
                maxlength="100"
                show-word-limit
                placeholder="请输入权限备注"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick type="primary" @click="submitForm"
          >确 定
        </el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </m-dialog>
  </div>
</template>

<script>
import {
  queryPower,
  createPower,
  updatePower,
  deletePower,
} from "@/api/system/powers";
import { getOption } from "@/api/system/options";
import { queryMenu } from "@/api/system/menus";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "powers",
  components: { Treeselect },
  data() {
    return {
      // 表格高度
      tableHeight: window.innerHeight - 275,
      // 遮罩层
      loading: true,
      // 返回结果集
      dataResult: [],
      // 菜单树选项
      menuOptions: [],
      // 权限描述选择框
      descriptionOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 合计条数
      total: 0,
      // 查询参数
      queryParams: {
        name: undefined,
        pageIndex: 1,
        pageSize: 20,
        orderby: "createTime",
        sort: "descending",
      },
      // 表单参数
      form: {
        menuId: "",
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "权限名称不能为空", trigger: "blur" },
        ],
        menuId: [
          {
            required: true,
            message: "权限所属的系统页面不能为空",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "权限描述不能为空", trigger: "blur" },
        ],
      },
      // 搜索条显示
      searchToggle: true,
    };
  },
  created() {
    // getOption("SYSTEM_POWER_DESCRIPTION").then(response => {
    //   this.descriptionOptions = response.data;
    //   console.log(response.data);
    // });
    this.getList();
    this.getTreeselect();
  },
  methods: {
    toggleRowExpansion_forAll(data, isExpansion) {
      data.forEach((item) => {
        this.$refs.dataTreeList.toggleRowExpansion(item, isExpansion);
        if (item.powers != undefined && item.powers != null) {
          this.toggleRowExpansion_forAll(item.powers, isExpansion);
        }
      });
    },
    toggleRowExpansion(isExpansion) {
      this.toggleRowExpansion_forAll(this.dataResult, isExpansion);
    },
    loadOptions({ action, parentNode, callback }) {},
    // 隐藏搜索
    handleSearch() {
      this.searchToggle = !this.searchToggle;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      queryPower(this.queryParams).then((response) => {
        console.log(response, "查询列表");

        // const tableCl = td => {
        //   if (td.powers.length > 0) {
        //   }
        // };
        const tableData = [];
        response.data.dataSource.forEach((i, index) => {
          console.log(i, "i");
          tableData.push({
            id: `p_${index}`,
            ...i,
            // ...i.powers.map(d => {
            //   return { ...d, menuId: i.menuId };
            // }),
            powers: i.powers.map((d) => {
              return { ...d, menuId: i.menuId };
            }),
            name: i.menuName,
          });
        });

        console.log(tableData, "处理后的数据");
        this.dataResult = tableData;
        this.total = response.data.totalCount;
        this.loading = false;
        console.log(this.dataResult, 1212312);
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.name,
        label: node.name,
        children: node.children,
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      queryMenu({}).then((response) => {
        let list = [];

        console.log(response, "获取到的数据");
        response.data.forEach((item, index) => {
          if (item.type == 1) {
            list.push({
              name: item.name,
              dataName: item.name,
              id: item.id,
            });
          } else {
            if (item.children) {
              item.children.forEach((el, i) => {
                if (el.type == 1) {
                  list.push({
                    name: item.name + "-" + el.name,
                    dataName: el.name,
                    id: el.id,
                  });
                }
              });
            }
          }
        });

        console.log(list, "菜单");
        this.menuOptions = list;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        menuId: undefined,
        name: undefined,
        page: undefined,
        description: undefined,
        remark: undefined,
      };
      // this.$nextTick(() => {
      //   this.$refs.form.clearValidate();
      // });
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryParams.name = "";
      this.queryParams.menuId = undefined;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 新增按钮操作 */
    handleCreate(row) {
      this.reset();
      this.form.menuId = this.queryParams.menuId ? this.queryParams.menuId : "";
      // console.log(this.form.menuId);

      this.getTreeselect();
      this.open = true;
      this.title = "添加权限";
    },
    /** 修改按钮操作 */
    handleUpdate(row, scope) {
      this.reset();
      this.getTreeselect();
      this.form = row;
      this.open = true;
      this.title = "修改权限";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            console.log(this.form, "form表单");
            updatePower(this.form).then((response) => {
              if (response.statusCode === 200) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.open = false;
                this.getList();
              }
            });
          } else {
            console.log(this.form, "form表单");
            createPower(this.form).then((response) => {
              if (response.statusCode === 200) {
                this.$message({
                  message: "新增成功",
                  type: "success",
                });
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletePower(row.id).then((response) => {
            if (response.statusCode === 200) {
              this.getList();
              this.$message({
                message: "删除成功",
                type: "success",
              });
            }
          });
        })
        .catch(function () {});
    },
    // 自动计算分页 Id
    handleIndexCalc(index) {
      return (
        (this.queryParams.pageIndex - 1) * this.queryParams.pageSize + index + 1
      );
    },
    // 排序操作
    handleSortable(val) {
      this.queryParams.orderby = val.prop;
      this.queryParams.sort = val.order;
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped src="./index.scss"></style>
